package com.xjtlu.manager.response;

import lombok.Data;

@Data
public class Result<T> {

    private static final Integer SUCCESS_CODE = 200;
    private static final String SUCCESS_MSG = "操作成功";
    private static final Integer FAIL_CODE = -2;

    private Integer code;
    private String message;
    private T data;

    public Result(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Result(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static Result<?> ok(){
        return new Result<>(SUCCESS_CODE, SUCCESS_MSG);
    }

    public static Result<?> ok(String message){
        return new Result<>(SUCCESS_CODE, message);
    }

    public static Result<?> ok(Integer code, String message){
        return new Result(code, message);
    }

    public static Result<?> ok(IErrorCode iErrorCode){
        return new Result(iErrorCode.getCode(), iErrorCode.getMessage());
    }

    public static <E> Result<E> ok(IErrorCode iErrorCode, E data){
        return new Result<E>(iErrorCode.getCode(), iErrorCode.getMessage(), data);
    }

    public static <E> Result<E> ok(E data){
        return new Result<E>(SUCCESS_CODE, SUCCESS_MSG, data);
    }

    public static <E> Result<E> ok(Integer code, String message, E data){
        return new Result<E>(code, message, data);
    }

    public static Result<?> error(){
        return new Result<>(ResultCode.SYSTEM_FAILED.getCode(), ResultCode.SYSTEM_FAILED.getMessage());
    }

    public static Result<?> error(ResultCode resultCode){
        return new Result<>(resultCode.getCode(), resultCode.getMessage());
    }

    public static <E> Result<E> error(ResultCode resultCode, E data){
        return new Result<E>(resultCode.getCode(), resultCode.getMessage(), data);
    }

    public static <E> Result<E> error(Integer code, String message){
        return new Result<E>(code, message);
    }

    public static Result<?> error(Exception exception){
        return new Result<>(FAIL_CODE, exception.getMessage());
    }

}
